package doencas;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import util.PersistenceMechanismException;
import util.PersistenceMechanismRDBMS;
import util.RepositorioException;

public class RepositorioDoencasBDR implements RepositorioDoencas{

	private static RepositorioDoencasBDR instance;
    private PersistenceMechanismRDBMS pm;
	
    private RepositorioDoencasBDR() {
        try {
            pm = PersistenceMechanismRDBMS.getInstance();
            pm.connect();
        }
        catch (Exception ex) {
            //System.out.println(ex.getMessage()); 
            ex.printStackTrace();
        }
    }
    public synchronized static RepositorioDoencasBDR getInstance() {
        if (instance == null) {
            instance = new RepositorioDoencasBDR();
        }
        return instance;
    }
	
	public void atualizar(Doenca doenca) throws DoencaNaoEncontradaException, RepositorioException {
		try {
    	    Statement statement = (Statement) pm.getCommunicationChannel();
            int i = statement.executeUpdate("UPDATE DOENCAS SET nome = '" + doenca.getNome() +
           	                                "', profilaxia = '" + doenca.getProfilaxia()+
           	                                "', tratamento = '" + doenca.getTratamento()+
           	                                "', sintomas = '" + doenca.getSintomas()+ 
           	                                "' WHERE codigo = '" + doenca.getCodigo() + "'");
            if (i == 0) {
            	throw new DoencaNaoEncontradaException(doenca.getCodigo());
            }
		} catch(PersistenceMechanismException e){
            throw new RepositorioException(e);
		} catch (SQLException e) {
		    throw new RepositorioException(e);
	    } finally {
	    	try {
				pm.releaseCommunicationChannel();
			} catch (PersistenceMechanismException ex) {
				throw new RepositorioException(ex);
			}
		}
		
	}

	public boolean existe(long codigo) throws RepositorioException {
		boolean resposta = false;
        try {
            Statement statement = (Statement) pm.getCommunicationChannel();
            ResultSet resultset = statement.executeQuery("SELECT * FROM DOENCAS WHERE codigo = '" + codigo + "'");
            resposta = resultset.next();
			resultset.close();
		} catch(PersistenceMechanismException e){
            throw new RepositorioException(e);
        } catch (SQLException e) {
			throw new RepositorioException(e);
		} finally {
			try {
				pm.releaseCommunicationChannel();
			} catch (PersistenceMechanismException ex) {
				throw new RepositorioException(ex);
			}
		}
        return resposta;
	}

	public IteratorDoencas getIterator() throws RepositorioException {
			return this.getDoencas().getIterator();
	}
	
	public RepositorioDoencas getDoencas() throws RepositorioException{
		RepositorioDoencas resposta = new RepositorioDoencasArray();
        try {
            Statement statement = (Statement) pm.getCommunicationChannel();
            ResultSet resultset = statement.executeQuery("SELECT * FROM DOENCAS");
            while (resultset.next()) {
                Doenca doenca = new Doenca(resultset.getLong("codigo"),
                						resultset.getString("nome"),
                						resultset.getString("profilaxia"),
                						resultset.getString("tratamento"),
                                        resultset.getString("sintomas"));
                resposta.inserir(doenca);
            }
			resultset.close();
		} catch(PersistenceMechanismException e){
            throw new RepositorioException(e);
        } catch (SQLException e) {
			throw new RepositorioException(e);
		} finally {
			try {
				pm.releaseCommunicationChannel();
			} catch (PersistenceMechanismException ex) {
				throw new RepositorioException(ex);
			}
		}
        return resposta;
	}

	public void inserir(Doenca doenca) throws RepositorioException {
		try {
            Statement statement = (Statement) pm.getCommunicationChannel();
            statement.executeUpdate("INSERT INTO DOENCAS VALUES ('" +
                                    doenca.getCodigo()+ "', '" +
                                    doenca.getNome()+ "', '" +
                                    doenca.getProfilaxia()+ "', '" +
                                    doenca.getTratamento()+ "', '" +
                                    doenca.getSintomas()+ "')");
        } catch(PersistenceMechanismException e){
            throw new RepositorioException(e);
        } catch (SQLException e) {
            throw new RepositorioException(e);
	    } finally {
		    try {
				pm.releaseCommunicationChannel();
			} catch (PersistenceMechanismException ex) {
				throw new RepositorioException(ex);
			}
        }
	}

	public Doenca procurar(long codigo) throws DoencaNaoEncontradaException, RepositorioException {
		Doenca doenca = null;
        try {
            Statement statement = (Statement) pm.getCommunicationChannel();
            ResultSet resultset = statement.executeQuery("SELECT * FROM DOENCAS WHERE codigo = '" + codigo + "'");
            if (resultset.next()) {
                doenca = new Doenca(resultset.getLong("codigo"),
                					resultset.getString("nome"),
                					resultset.getString("profilaxia"),
                					resultset.getString("tratamento"),
                					resultset.getString("sintomas"));
            } else {
            	throw new DoencaNaoEncontradaException(codigo);
            }
			resultset.close();
		} catch(PersistenceMechanismException e){
            throw new RepositorioException(e);
        } catch (SQLException e) {
			throw new RepositorioException(e);
		} finally {
			try {
				pm.releaseCommunicationChannel();
			} catch (PersistenceMechanismException ex) {
				throw new RepositorioException(ex);
			}
		}
		return doenca;
	}

	public RepositorioDoencas procurar(Doenca doenca) throws RepositorioException {
		RepositorioDoencas resposta = new RepositorioDoencasArray();
		try {
            Statement statement = (Statement) pm.getCommunicationChannel();
            ResultSet resultset = statement.executeQuery("SELECT * FROM DOENCAS");
            while (resultset.next()) {
            	doenca = new Doenca(resultset.getLong("codigo"),
                                           resultset.getString("nome"),
                                           resultset.getString("profilaxia"),
                                           resultset.getString("tratamento"),
                                           resultset.getString("sintomas"));
                resposta.inserir(doenca);
            }
			resultset.close();
		} catch(PersistenceMechanismException e){
            throw new RepositorioException(e);
        } catch (SQLException e) {
			throw new RepositorioException(e);
		} finally {
			try {
				pm.releaseCommunicationChannel();
			} catch (PersistenceMechanismException ex) {
				throw new RepositorioException(ex);
			}
		}
        return resposta;
	}

	public void remover(long codigo) throws DoencaNaoEncontradaException, RepositorioException {
		try{
            Statement statement = (Statement) pm.getCommunicationChannel();
		    int i = statement.executeUpdate("DELETE FROM DOENCAS WHERE codigo = '" + codigo + "'");
            if (i == 0) {
            	throw new DoencaNaoEncontradaException(codigo);
            }
		} catch(PersistenceMechanismException e){
            throw new RepositorioException(e);
		} catch(SQLException e){
            throw new RepositorioException(e);            
		} finally {
			try {
				pm.releaseCommunicationChannel();
			} catch (PersistenceMechanismException ex) {
				throw new RepositorioException(ex);
			}
		}
		
	}
	

}
